package com.lly.songhigh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.lly.songhigh.R;
import com.lly.songhigh.application.BaseApplication;
import com.lly.songhigh.constant.StringResouce;

/**
 * Created by dllo on 15/10/23.
 */
public class WebViewActivity extends BaseActivity implements View.OnClickListener {

    private WebView webView;
    private Toolbar webViewToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        BaseApplication.getInstance().addActivity(this);
        webView = (WebView) findViewById(R.id.webView);
        webViewToolbar = (Toolbar) findViewById(R.id.webView_toolbar);

        Intent intent = getIntent();
        String link = intent.getStringExtra("link");
        String desc = intent.getStringExtra("dese");
        webViewToolbar.setTitle(desc);
        webViewToolbar.setNavigationIcon(R.mipmap.toolbar_back);
        webViewToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 从webView中获取WebSettings设置对象
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDisplayZoomControls(true);
        settings.setSupportZoom(true);
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                WebViewActivity.this.setProgress(newProgress * 1000);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Toast.makeText(WebViewActivity.this, StringResouce.WEBPAGE_LOAD_ERROR, Toast.LENGTH_SHORT).show();
            }
        });

        // 设置要加载的网页地址
        webView.loadUrl(link);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
    }
}
